package com.easylinkin.linkappapi.meterbilling.config.verification;

import com.easylinkin.linkappapi.meterbilling.config.MeterUsernamePasswordAuthenticationFilter;
import com.easylinkin.linkappapi.meterbilling.config.WechatPublicAuthenticationFailureHandler;
import com.easylinkin.linkappapi.meterbilling.config.WechatPublicAuthenticationSuccessHandler;
import com.easylinkin.linkappapi.meterbilling.service.MeterCodeLoginService;
import com.easylinkin.linkappapi.meterbilling.service.MeterLoginService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpStatus;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.authentication.HttpStatusEntryPoint;
import org.springframework.security.web.csrf.CsrfFilter;
import org.springframework.web.filter.CharacterEncodingFilter;

import javax.annotation.Resource;

/**
 * class info :水电表微信公众号短信验证码登录config
 *
 * @author liuqihang
 * @date 2021/7/31 16:15
 */
@Configuration
@EnableWebSecurity
@Order(4)
public class SmsCodeAuthenticationSecurityConfig extends WebSecurityConfigurerAdapter {

    @Resource
    MeterCodeLoginService meterCodeLoginService;
    /**
     * 注册用户认证服务
     */
    @Override
    public MeterCodeLoginService userDetailsService() {
        return meterCodeLoginService;
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(meterCodeLoginService);
    }

    @Bean
    SmsCodeAuthenticationFilter smsCodeAuthenticationFilter() throws Exception {
        SmsCodeAuthenticationFilter filter = new SmsCodeAuthenticationFilter();
        filter.setAuthenticationManager(authenticationManagerBean());
        filter.setAuthenticationSuccessHandler(wechatPublicAuthenticationSuccessHandler);
        filter.setAuthenticationFailureHandler(wechatPublicAuthenticationFailureHandler);
        return filter;
    }

    @Resource
    private WechatPublicAuthenticationFailureHandler wechatPublicAuthenticationFailureHandler;

    @Resource
    private WechatPublicAuthenticationSuccessHandler wechatPublicAuthenticationSuccessHandler;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        CharacterEncodingFilter filter = new CharacterEncodingFilter();
        filter.setEncoding("UTF-8");
        filter.setForceEncoding(true);
        http.addFilterBefore(filter, CsrfFilter.class);
        http
                .csrf().disable()
                .exceptionHandling().authenticationEntryPoint(new HttpStatusEntryPoint(HttpStatus.UNAUTHORIZED))
                .and()
                .authorizeRequests()
                .antMatchers("/swagger-ui.html", "/webjars/**", "/swagger-resources/**", "/v2/api-docs/**",
                        "/user/verificationCode", "/user/sendVerificationCode", "/user/verificationMessages","/user/resetPassword",
                        "/linkappUser/verificationCode","/adminUser/verificationCode","/adminUser/isEnableVerificationCode", "/linkappUser/sendVerificationCode", "/linkappUser/verificationMessages","/linkappUser/resetPassword",
                        "/program/getProgramInfo/*", "/lang", "/checkhealth","/datapush",
                        "/synchronizationLinkthing","/ssoLogin","/tenant/getSSOUrl","/tenant/addLinkappTenant","/adminlogin","/application/getPersonalIfo",
                        "/personality/getPersonalIfo","/onlineStateChange", "/anon/**", "/miniappLogin","/linkappUser/verificationCode/**","/phoneLogin","/adminPhoneLogin", "/meterLogin", "/meterCodeLogin", "/recharge/thirdAsynNotify", "/resident/verificationCode", "/resident/checkVerificationCode", "/resident/updatePwdByForget",
                        "/selectAllPrivilegeByApplication","/selectPrivilegeByApplication","/selectPersonalityByApplication","/initLinkappTenant","/updatePrivilegePersonalityByApplication",
                        "/getTenantInfo","/updateTenantAccount")
                .permitAll()
                // 接口调试阶段 目前不校验接口 modify by tongjie
                .anyRequest().authenticated().and()
                // 表单登录配置
                .formLogin()
                .successHandler(wechatPublicAuthenticationSuccessHandler)
                // 登录失败处理
                .failureHandler(wechatPublicAuthenticationFailureHandler);
    }

}
